﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.BasicService;
using RN.ERP.Web.ModelService;
using Newtonsoft.Json;

namespace RN.ERP.Web.UserControl
{
    public partial class UCPsnQual : System.Web.UI.UserControl
    {
        #region 属性
        /// <summary>
        /// 角色
        /// </summary>
        private string _setRole;
        public string SetRole
        {
            set
            {
                _setRole = value;
            }
        }
        /// <summary>
        /// 证书类型
        /// </summary>
        private string _setZgzs;
        public string SetZgzs
        {
            set
            {
                _setZgzs = value;
            }
        }
        /// <summary>
        /// Parentid
        /// </summary>
        private string _setParentid;
        public string SetParentid
        {
            set
            {
                _setParentid = value;
            }
        }
        /// <summary>
        /// 资质guid
        /// </summary>
        private string _setQualGuid;
        public string SetQualGuid
        {
            set
            {
                _setQualGuid = value;
            }
        }
        /// <summary>
        /// 传到前台最终选择的证书code
        /// </summary>
        public string Zscode
        {
            get
            {
                return this.zscode.Value;
            }
        }
        /// <summary>
        /// 传到前台最终选择的证书等级
        /// </summary>
        public string Zsdj
        {
            get
            {
                return this.levelcode.Value;
            }
        }
        /// <summary>
        /// 传到前台最终选择的专业code
        /// </summary>
        public string Zycode
        {
            get
            {
                return this.zycode.Value;
            }
        }
        #endregion 属性
        BasicServiceClient client = new BasicServiceClient();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            PsnQulaDDValue qualinfo = JsonConvert.DeserializeObject<PsnQulaDDValue>(client.GetPersonQualityByGid(_setQualGuid, "10"));

            if (!IsPostBack)
            {
                //判断下拉是通过前台js加载还是后台加载
                if (qualinfo != null && _setQualGuid != Guid.Empty.ToString() && !string.IsNullOrEmpty(_setQualGuid))
                {
                    this.isJs.Value = "0";
                    DDBind(qualinfo);
                }
                else
                {
                    //前台js获取isJs的值用来判断是否进行js进行加载数据
                    this.isJs.Value = "1";
                }

            }
        }
        /// <summary>
        /// 绑定下拉
        /// </summary>
        public void DDBind(PsnQulaDDValue PsnQual)
        {
            IList<DictQualityInfo> dicType = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic("zgzs", "0"));
            foreach (var item in dicType)
            {
                item.Code = item.Code + "@" + item.Id;
            }
            this.psnzstype.DataSource = dicType;
            this.psnzstype.DataTextField = "Title";
            this.psnzstype.DataValueField = "Code";
            this.psnzstype.SelectedValue = PsnQual.dicZsType;
            this.psnzstype.DataBind();

            IList<DictQualityInfo> dicLev = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic("zgdj", PsnQual.dicZsType.Split(new char[] { '@' })[1]));
            foreach (var item in dicLev)
            {
                item.Code = item.Code + "@" + item.Id;
            }
            if (!string.IsNullOrEmpty(PsnQual.dicZsLev))
            {
                this.psnzslev.DataSource = dicLev;
                this.psnzslev.DataTextField = "Title";
                this.psnzslev.DataValueField = "Code";
                this.psnzslev.SelectedValue = PsnQual.dicZsLev;
                this.psnzslev.DataBind();
            }
            else
            {
                ListItem DefaultItem = new ListItem("请选择", "-1");
                this.psnzslev.Items.Insert(0, DefaultItem);
                this.psnzslev.DataSource = dicLev;
                this.psnzslev.DataTextField = "Title";
                this.psnzslev.DataValueField = "Code";
                this.psnzslev.DataBind();
            }

            IList<DictQualityInfo> dicSpe = JsonConvert.DeserializeObject<IList<DictQualityInfo>>(client.GetQualitiesDic("specialty", "0"));
            foreach (var item in dicSpe)
            {
                item.Code = item.Code + "@" + item.Id;
            }
            this.psnzy.DataSource = dicSpe;
            this.psnzy.DataTextField = "Title";
            this.psnzy.DataValueField = "Code";
            if (string.IsNullOrEmpty(PsnQual.dicZsSpe))
            {
                ListItem DefaultItem = new ListItem("请选择", "-1");
                this.psnzy.Items.Insert(0, DefaultItem);
            }
            else
            {
                this.psnzy.SelectedValue = PsnQual.dicZsSpe;
            }
            this.psnzy.DataBind();
        }
    }
}